/**
 * Created by Nicolas at 2015-08-24
 */
grApp.filter('filterAccount',function(){
    var result='';
    return function(item){
        if(parseInt(item)<20){
            result='差'+(20-item)+"元配送";
        }else {
            result='立即结算';
        }
        return result;
    };
});
grApp.controller('JuicePackageCtrl',['$scope','$http','toaster','JuiceFactory',function($scope,$http,toaster,juiceFactory){
    //添加到我的套餐
    var specID = "";
    var proId = "";
    $scope.addToPackage = function(pid){
        $scope.data={size:1};
        var $modal = angular.element("#addNewProToPackage");
        $modal.modal();
        //展示商品
        juiceFactory.getProduct(pid).then(function(data){
            $scope.proName = data.pro.name ;
            proId = data.pro.objectId ;
            $scope.minSpec = data.pro.minSpec ;
            specID =  data.spec.psid;
            //console.log($scope.minSpec);
            $scope.radio = {isChecked:$scope.minSpec};
            //$scope.OldPrice = specID[$scope.minSpec].price;
            for(x in data.spec ){
                if(data.spec[x].psid === $scope.minSpec){
                    $scope.price = data.spec[x].spec.price;
                }
            }
            $scope.proSpec = data.spec ;
            $scope.proId = data.pro.id ;
        });
    }
    $scope.RunData = function(){
        $scope.data={size:1};
        $scope.accountAll = 0;

        var account = 0;
        juiceFactory.getPackageList().then(function(data){
            $scope.myPackageList = data.info;
            $scope.isPackage = 0;
            for(x in data.info ){
                if(data.info[x].cid===19){
                    $scope.isPackage=1;
                }
            }
            for(x in data.info ){
                //console.log(data.info[x].cid );
                /**
                 *TODO:
                 *
                 */
                //这里先写死，后期修改
                if($scope.isPackage===1){
                    $scope.accountAll = parseInt($scope.accountAll)+parseInt(data.info[x].psPrice)*parseInt(data.info[x].count);
                }else{
                    $scope.accountAll = parseInt($scope.accountAll)+parseInt(data.info[x].psGrprice)*parseInt(data.info[x].count);
                }
                //console.log(data.info[x].count)
                account = parseInt(account)+parseInt(data.info[x].psGrprice)*parseInt(data.info[x].count);
            }
            $scope.account =account- $scope.accountAll;
            if(parseInt($scope.accountAll)<20){
                $scope.accountNow = 0 ;
            }else{
                $scope.accountNow = 1;
            }
        });
    }
    $scope.RunData();
    //修改套餐
    $scope.updatePackage = function(pid,psid){
        specID =  psid;
        var $modal = angular.element("#updatePackage");
        $modal.modal();
        //展示商品,默认选中已经选择的规格和数量
        //展示商品
        juiceFactory.getProduct(pid).then(function(data){
            $scope.proName = data.pro.name ;
            proId = data.pro.objectId ;
            $scope.minSpec = data.pro.minSpec ;

            specID =  data.spec.psid;
            //console.log($scope.minSpec);
            $scope.radio = {isChecked:psid};

            //$scope.OldPrice = specID[$scope.minSpec].price;
            for(x in data.spec ){
                if(data.spec[x].psid === psid){
                    $scope.oldPrice = data.spec[x].spec.price;
                }
            }
            $scope.proSpec = data.spec ;
            $scope.proId = data.pro.id ;
        });

    }
    //保存到juicepackage表中
    $scope.savePackage = function(count){
        specID = $scope.radio.isChecked
        juiceFactory.savePackage(proId,specID,count).then(function(data){
            //console.log('success');
            if(data.code === 0){
                $scope.RunData();//重新加载购物车数据
                //console.log('success');
            }else{
                $scope.RunData();//重新加载购物车数据
                //console.log("err");
            }
        });
    }

    //从套餐中删除
    $scope.deletePro = function(pid,psid){
        juiceFactory.deleteProFromPackage(pid,psid).then(function(data){
            if(data.code === 0){
                $scope.RunData();//重新加载购物车数据
                //console.log('success');
            }else{
                $scope.RunData();//重新加载购物车数据
                //console.log("err");
            }
        });
    }
    $scope.select = function(args,args1){

        $scope.data={size:1};
        specID = args1;
        $scope.price=args;
        $scope.radio={isChecked:specID}
    };
    /**
     * 减少数量
     */
    $scope.minus = function(){
        var mini = 1;
        $scope.data.size = (parseInt($scope.data.size) > mini)?(parseInt($scope.data.size)-1):mini;
    };
    /**
     *  增加购买数量
     */
    $scope.plus = function(){
        var size = $scope.data.size;
        var maxSize = 99;
        $scope.data.size = (parseInt(size) < maxSize)?(parseInt(size)+1):maxSize;

    };

    $scope.orderNow = function(accountAll,isPackage){
        window.location.href='/order/confirmPackageOrder/0/'+accountAll+'/'+isPackage;
    }
}]);
grApp.factory('JuiceFactory',['$http','$q',function($http,$q){
    var service = {};
    //取消、添加收藏
    service.getProduct = function(pid){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/juice/getPro',
            params:{pid:pid}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    service.savePackage= function (pid,psid,count) {
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/juice/savePackage',
            params:{pid:pid,psid:psid,count:count}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    service.getPackageList = function(){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/juice/getPackageList'
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    service.deleteProFromPackage = function(pid,psid){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            method: 'GET',
            url: '/juice/deleteFromPackage',
            params:{pid:pid,psid:psid}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };

    return service;
}]);
